<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ConfirmController
 *
 * @author urech
 */
class ConfirmController extends Controller {

	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout = '//layouts/column2';

	/**
	 * @return array action filters
	 */
	public function filters() {
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules() {
		return array(
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions' => array('accept', 'acceptModified', 'refuse', 'done'),
				'roles' => array('admin'),
			),
			array('deny', // deny all users
				'users' => array('*'),
			),
		);
	}

	public function actionAccept() {
		$model = $this->loadModel($_GET['key']);
		if ($model === null) {
			echo "Key not valid";
		} else {
			$model->confirmationDate = date('Y-m-d h:i:s');
			if ($model->update(array('confirmationDate'))) {
				$this->sendUserConfirmationMail($model, 'durchgeführt');
				$this->redirect(array('done', 'id' => $model->Benutzernummer));
			}
		}
	}

	public function actionAcceptModified() {
		$model = $this->loadModel($_GET['key']);
		if ($model === null) {
			echo "Key not valid";
		} else {
			if (isset($_POST['AttributeChange'])) {
				$model->attributes = $_POST['AttributeChange'];
				$model->confirmationDate = date('Y-m-d H:i:s');
				if ($model->update()) {
					$this->sendUserConfirmationMail($model, 'modifiziert durchgeführt');
					$this->redirect(array('done', 'id' => $model->Benutzernummer));
				} 
			}
			if (isset($_POST['MembershipChange'])) {
				$model->attributes = $_POST['MembershipChange'];
				$model->confirmationDate = date('Y-m-d H:i:s');
				if ($model->update()) {
					$this->sendUserConfirmationMail($model, 'modifiziert durchgeführt');
					$this->redirect(array('done', 'id' => $model->Benutzernummer));
				}
			}

			$this->render('modified', array(
				'model' => $model,
			));
		}
	}

	public function actionRefuse() {
		$model = $this->loadModel($_GET['key']);
		if ($model === null) {
			echo "Key not valid";
		} else {
			if (isset($_POST['AttributeChange'])) {
				$model->attributes = $_POST['AttributeChange'];
				$model->confirmationDate = date('Y-m-d h:i:s');
				if ($model->update()) {
					$this->sendUserConfirmationMail($model, 'leider abgelehnt');
					$this->redirect(array('done', 'id' => $model->Benutzernummer));
				}
			}
			if (isset($_POST['MembershipChange'])) {
				$model->attributes = $_POST['MembershipChange'];
				$model->confirmationDate = date('Y-m-d H:i:s');
				if ($model->update()) {
					$this->sendUserConfirmationMail($model, 'leider abgelehnt');
					$this->redirect(array('done', 'id' => $model->Benutzernummer));
				}
			}

			$this->render('refused', array(
				'model' => $model,
			));
		}
	}

	public function actionDone($id) {
		$model = Member::model()->findByPk($id);
		$this->render('done', array(
			'model' => $model,
		));
	}

	private function sendUserConfirmationMail($model, $status) {
		$user = User::model()->with('profile')->findByPk($model->userId);
		$member = Member::model()->findByPk($model->Benutzernummer);

		$message = new YiiMailMessage();
		$message->view = 'userConfirmation';
		$message->setBody(array('model' => $model, 'user' => $user, 'member' => $member, 'status' => $status), 'text/plain');

		$message->addTo($user->email);
		$message->from = Yii::app()->params['supportEmail'];
		$message->setSubject("Änderung aus Sektionsportal abgeschlossen");

		Yii::app()->mail->send($message);
	}

	private function loadModel($key) {
		$model = AttributeChange::model()->find("confirmationKey = :key AND confirmationDate = '0000-00-00 00:00:00'", array(":key" => $key));
		if ($model === null){
			$model = MembershipChange::model()->find("confirmationKey = :key AND confirmationDate = '0000-00-00 00:00:00'", array(":key" => $key));
		}

		return $model;
	}

}

?>
